<?php
Class module_user extends module {
	
	function menu() {
		$this->output();
	}
	
	function infobox() {
		$user = new user();
		$res = $user->loadFromSession();
		if ($res) {
			$this->attach("user", $user);
			$this->output("infobox");	
		} else {
			$this->output("loginform");
		}
	}
	
	function loginform() {
		$this->output("loginform");
	}
	
	function registerform() {
		$this->output("registerform");
	}
	
	function login() {
		$user = new user();
		$res = $user->login(request::get('name', 'post'), request::get('password', 'post'));
		if (!$res) {
			message::setFlash("Login failed");
		} else {
			message::setFlash("Logged in succesfull");
		}
		document::getInstance()->setRedirect(true);
	}
	
	function logout() {
		$user = new user();
		$res = $user->logout();
		message::setFlash("Logged out succesfull");
		document::getInstance()->setRedirect(document::REDIRECT_REFFERER);
	}
	
	function register() {
		$user = new user();
		if ($user->insert(request::get("*", "post"))) {
			message::setFlash("User created");
			document::getInstance()->setRedirect(document::REDIRECT_REFFERER);
		} else {
			message::setMessage("Something went wrong...");
			$this->registerform();
		}
	}
}
?>